﻿using System.Windows;
using System.Windows.Controls;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Controls
{
    /// <summary>
    /// Interaction logic for CeremonyValuesControl.xaml
    /// </summary>
    public partial class CeremonyValuesControl
    {
        public CeremonyValuesControl()
        {
            InitializeComponent();
        }

        public CeremonyValuesPresentationModel Model
        {
            get { return DataContext as CeremonyValuesPresentationModel; }
            set { DataContext = value; }
        }

        private void TextBoxLanguage_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if(!string.IsNullOrEmpty(TextBoxNewLanguage.Text))
            {
                if (Model.NewLanguage.Id == 0)
                {
                    ButtonAddLanguage.IsEnabled = true;
                }
            }
        }

        private void TextBoxSpecialNeeds_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(TextBoxSpecialNeeds.Text))
            {
                ButtonAddSpecialNeed.IsEnabled = true;
            }
        }

        private void TextBoxLanguage_LostFocus(object sender, RoutedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && !string.IsNullOrEmpty(TextBoxNewLanguage.Text))
            {
                if (model.NewLanguage.Id == 0)
                {
                    model.NewLanguage = new ValueIdPair {Name = TextBoxNewLanguage.Text};
                }
                else
                {
                    model.NewLanguage.Name = TextBoxNewLanguage.Text;
                }
            }
        }

        private void TextBoxSpecialNeeds_LostFocus(object sender, RoutedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && !string.IsNullOrEmpty(TextBoxSpecialNeeds.Text))
            {
                model.NewSpecialNeed = new ValueIdPair { Name = TextBoxSpecialNeeds.Text };
            }
        }

        private void ListBoxCeremonyAllLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && ListBoxCeremonyAllLanguages.SelectedItem != null)
            {
                ButtonAddCeremonyLanguage.IsEnabled = IsNotAlreadyAdded((ValueIdPair)ListBoxCeremonyAllLanguages.SelectedItem , ListBoxCeremonyLanguages);
                ButtonDeleteCeremonyLanguage.IsEnabled = false;
                model.NewCeremonyLanguage = (ValueIdPair)ListBoxCeremonyAllLanguages.SelectedItem;
            }
        }

        private void ListBoxCertificateAllLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && ListBoxCertificateAllLanguages.SelectedItem != null)
            {
                ButtonDeleteCertificateLanguage.IsEnabled = false;
                ButtonAddCertificateLanguage.IsEnabled = IsNotAlreadyAdded((ValueIdPair)ListBoxCertificateAllLanguages.SelectedItem, ListBoxCertificateLanguages);
                model.NewCertificateLanguage = (ValueIdPair) ListBoxCertificateAllLanguages.SelectedItem;
            }
        }

        private void ListBoxCeremonyLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && ListBoxCeremonyLanguages.SelectedItem != null)
            {
                ButtonAddCeremonyLanguage.IsEnabled = false;
                ButtonDeleteCeremonyLanguage.IsEnabled = IsDeleteableLanguage((ValueIdPair)ListBoxCeremonyLanguages.SelectedItem);
                model.NewCeremonyLanguage = (ValueIdPair)ListBoxCeremonyLanguages.SelectedItem;
            }
        }

        private static bool IsDeleteableLanguage(ValueIdPair selectedLanguage)
        {
            if(selectedLanguage.Name.ToLower().Equals("svenska") || selectedLanguage.Name.ToLower().Equals("engelska"))
            {
                return false;
            }
            return true;
        }

        private static bool IsNotAlreadyAdded(ValueIdPair selectedLanguage, ListBox listBox)
        {
            foreach (ValueIdPair item in listBox.Items)
            {
                if(item.Name.Equals(selectedLanguage.Name))
                    return false;
            }
            return true;
        }

        private void ListBoxLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && ListBoxLanguages.SelectedItem != null)
            {
                ButtonAddLanguage.IsEnabled = false;
                var selectedLanguage = (ValueIdPair) ListBoxLanguages.SelectedItem;
                ButtonChangeLanguage.IsEnabled = IsDeleteableLanguage(selectedLanguage);
                ButtonDeleteLanguage.IsEnabled = IsNotAlreadyAdded(selectedLanguage, ListBoxCeremonyLanguages) 
                                                 && IsNotAlreadyAdded(selectedLanguage, ListBoxCertificateLanguages)
                                                 && IsNotAlreadyAdded(selectedLanguage, ListBoxReportLanguages);
                ButtonNewLanguage.IsEnabled = true;
                TextBoxNewLanguage.Text = selectedLanguage.Name;
                model.NewLanguage = selectedLanguage;
            }
        }

        private void ListBoxCertificateLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null && ListBoxCertificateLanguages.SelectedItem != null)
            {
                ButtonAddCertificateLanguage.IsEnabled = false;
                var selectedLanguage = (ValueIdPair)ListBoxCertificateLanguages.SelectedItem;
                ButtonDeleteCertificateLanguage.IsEnabled = IsDeleteableLanguage(selectedLanguage);
                model.NewCertificateLanguage = selectedLanguage;
            }
        }

        private void ButtonNewLanguage_Click(object sender, RoutedEventArgs e)
        {
            var model = DataContext as CeremonyValuesPresentationModel;

            if (model != null)
            {
                model.NewLanguage = new ValueIdPair();
                TextBoxNewLanguage.Text = "";
            }
            ButtonChangeLanguage.IsEnabled = false;
            ButtonDeleteLanguage.IsEnabled = false;
            ButtonAddLanguage.IsEnabled = false;
            ButtonNewLanguage.IsEnabled = false;
        }

        private void ButtonDeleteLanguage_Click(object sender, RoutedEventArgs e)
        {
            ButtonChangeLanguage.IsEnabled = false;
            ButtonDeleteLanguage.IsEnabled = false;
            ButtonAddLanguage.IsEnabled = false;
            ButtonNewLanguage.IsEnabled = false;
        }
    }
}
